package com.cloudsigns.factorymanager2.project.customview;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.adapter.GeneralPurposePopupWindowAdapter;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

/**
 * 通用下拉框
 * Created by tony on 2017/12/12.
 */

public class GeneralPurposePoupWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private View conentView;
    private ListView lv;
    private String datas[] = {};
    private GeneralPurposePopupWindowAdapter adapter;
    private Context context;
    private ItemClickListener listener;

    public GeneralPurposePoupWindow(final Activity context, String datas[]) {
        super(context);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.poupwindow_general_purpose, null);
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.AnimationPreview);

        this.datas = datas;

        lv = conentView.findViewById(R.id.lv);
        adapter = new GeneralPurposePopupWindowAdapter(context, datas);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }


    public void setItemClick(ItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 显示popupWindow
     *
     * @param anchor
     */
    public void showPopupWindow(View anchor) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(anchor, anchor.getLayoutParams().width / 8, 5);// 显示popupwindow的位�?
        } else {
            this.dismiss();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        if (listener != null)
            listener.itemClick(view,position);

        if (this.isShowing())
            this.dismiss();


    }
}
